package cn.iocoder.yudao.module.reading.controller.app.community;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.common.LikeToggleRespVO;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.work.WorkRespVO;
import cn.iocoder.yudao.module.reading.service.community.CommunityWorkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 用户 APP - 社区作品 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "用户 APP - 社区作品")
@RestController
@RequestMapping("/reading/community/works")
@Validated
@Slf4j
public class CommunityWorkController {

    @Resource
    private CommunityWorkService workService;

    @GetMapping("/{workId}")
    @Operation(summary = "获取作品详情")
    public CommonResult<WorkRespVO> getWorkDetail(
            @Parameter(description = "作品ID", required = true) @PathVariable("workId") Long workId,
            @Parameter(description = "作品类型", required = true) @RequestParam("workType") String workType) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        WorkRespVO workDetail = workService.getWorkDetail(workId, workType, userId);
        return success(workDetail);
    }

    @GetMapping("/permission/{workId}")
    @Operation(summary = "检查作品权限")
    @PreAuthenticated
    public CommonResult<String> checkWorkPermission(
            @Parameter(description = "作品ID", required = true) @PathVariable("workId") Long workId,
            @Parameter(description = "作品类型", required = true) @RequestParam("workType") String workType) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        String permission = workService.checkWorkPermission(workId, workType, userId);
        return success(permission);
    }

    @PostMapping("/{workId}/collect")
    @Operation(summary = "切换作品收藏状态")
    @PreAuthenticated
    public CommonResult<LikeToggleRespVO> toggleWorkCollection(
            @Parameter(description = "作品ID", required = true) @PathVariable("workId") Long workId,
            @Parameter(description = "作品类型", required = true) @RequestParam("workType") String workType) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        LikeToggleRespVO result = workService.toggleWorkCollection(workId, workType, userId);
        return success(result);
    }

}
